package com.pet.server.ext.action.home;


import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.BeanToJson;
import com.pet.server.ext.Result;
import com.pet.server.ext.data.Constant;
import com.pet.server.ext.data.Page;
import com.pet.server.ext.data.ProductInfo;
import com.pet.server.ext.service.ArticleService;
import com.pet.server.ext.service.ProductService;
import com.pet.server.ext.util.JsonUtil;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;
import com.pet.server.ext.util.nuomi.NuomiClient;

public class Product extends ActionBase{

	public  String shipin_code="005002";
	public  String yongpin_code="005001";
	public  String fuwu_code="005003";
	public Integer startIndex,itemCount,radius;
	public String queryString,cate,city,district,city_name,keyword,latitude,longitude;
	public Double min_price,max_price;
	private String[] pet_cate,product_cate,product_age,product_brand;
	
	public Page page;
	public Product(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		cate = params.getFieldValue("cate");
		startIndex = NumberUtil.getInteger(params.getFieldValue("startIndex"), 0);
		itemCount =  NumberUtil.getInteger(params.getFieldValue("itemCount"), 10);
		radius = NumberUtil.getInteger(params.getFieldValue("radius"),10);
		latitude = params.getFieldValue("latitude");
		longitude = params.getFieldValue("longitude");
		city = params.getFieldValue("city");
		district = params.getFieldValue("district");
		queryString = params.getFieldValue("queryString");
		city_name = params.getFieldValue("city_name");
		keyword = params.getFieldValue("keyword");
		
		pet_cate=StringUtil.isEmpty(params.getFieldValue("pet_cate"))?null: params.getFieldValue("pet_cate").split(",");
		product_cate=StringUtil.isEmpty(params.getFieldValue("product_cate"))?null: params.getFieldValue("product_cate").split(",");
		product_age = StringUtil.isEmpty(params.getFieldValue("product_age"))?null: params.getFieldValue("product_age").split(",");
		product_brand = StringUtil.isEmpty(params.getFieldValue("product_brand"))?null: params.getFieldValue("product_brand").split(",");
		min_price = NumberUtil.getDouble(params.getFieldValue("min_price"));
		max_price = NumberUtil.getDouble(params.getFieldValue("max_price"));
		page = new Page(startIndex, itemCount,queryString);
	}
	
	@Override
	public Result invokeService() throws ApiError {
		JSONObject o = new JSONObject();
		ProductService productService = new ProductService();
		ArticleService articleService = new ArticleService();
		
		if("001003".equals(cate)){
			JSONArray array = new JSONArray();
			String location = null;
			if(!StringUtil.isEmpty(longitude,latitude)){
				location=longitude+","+latitude;
			}
			
			JSONObject dataJson = NuomiClient.searchDeals(city_name, location, keyword, page.startIndex+1, page.itemCount, "8000");
			
			Integer errno = JsonUtil.getValue(dataJson,"errno", Integer.class);
		    if(errno==0){
		    	JSONArray json_array = dataJson.getJSONObject("data").getJSONArray("deals");//("");
		    	Integer total = dataJson.getJSONObject("data").getInt("total");
		    	page.setTotalCount(total);
		    	for(int index=0;index<json_array.size();index++){
		    		JSONObject json = new JSONObject();
		    		JSONObject so = json_array.getJSONObject(index);
		    		json.put("deal_id", so.getString("deal_id"));//团单id,商户描述
		    		json.put("image", so.getString("image"));//图片
		    		json.put("tiny_image", so.getString("tiny_image"));//小图
		    		json.put("title", so.getString("title"));//商户标题
		    		json.put("min_title", so.getString("min_title"));
		    		json.put("is_reservation_required", so.getBoolean("is_reservation_required"));
		    		json.put("description", so.getString("description"));//商户描述
		    		json.put("market_price",   so.getInt("market_price")/100d);//市场价 (单位:分)
		    		json.put("current_price", so.getInt("current_price")/100d);//当前团购价
		    		json.put("promotion_price", so.getInt("promotion_price")/100d);//促销价
		    		json.put("score", so.getDouble("score"));//评分
		    		json.put("sale_num", so.getInt("sale_num"));//已售数
		    		json.put("deal_murl", so.getString("deal_murl"));//url
		    		json.put("publish_time", so.getInt("publish_time"));//团单发布时间,
		    		json.put("purchase_deadline", so.getInt("purchase_deadline"));//团单购买最后期限
		    		json.put("distance", so.getDouble("distance"));//距离
		    		json.put("source", "nuomi");
		    		JSONArray shop_array = so.getJSONArray("shops");
		    		if(shop_array.size()>0){
		    			json.put("shop", shop_array.getJSONObject(0));
		    		}
		    		array.add(json);
		    	}
		    	Result result = new Result();
		        result.setPage4array(page, array);
		        //result.put("data", o);
			    return result;
		    }
		    /**
			List<XmlData> list = null;
			String strs=null;
			if(!StringUtil.isEmpty(latitude,longitude)){
				strs = MeiTuanApi.callMeiTuanByDili(latitude, longitude, null, page.itemCount, null, page.startIndex, radius);
			}else{
				strs=MeiTuanApi.callMeiTuanByCity(strs, district, null, page.itemCount, null, page.startIndex);
			}
			list = XMLParse.parseXmlToList(strs);
			for(XmlData d:list){
				d.distance=DistanceEstimator.route(latitude, longitude, "", d.shop_lat, d.shop_long, "");
				d.source=Status.Source.meituan.toString();
			}
			
			page.list=list;//coverToProduct(list);
			*/
		}else{
			productService.searchProductInfo(page, cate, pet_cate, product_cate, product_age, product_brand, min_price, max_price);
		}
		//List<Constant> list = articleService.queryConstant(pinpai);
		List<Constant> fenlei = articleService.queryConstant(cate);
		//o.put("pinpai", BeanToJson.listTojson(list, "id","pcode","val"));
		for(Constant lei:fenlei){
			lei.pinpai=articleService.queryConstantByCate(lei.code);
			lei.list=articleService.queryConstant(lei.code);
		}
		
		
		
		o.put("fenlei", BeanToJson.listTojson(fenlei,"pcode","val"));
		Result result = new Result();

		List<ProductInfo> list = (List<ProductInfo>) page.getList();
		o.put("list", JsonUtil.product2JsonArray(list,"pcode","val","product_detail"));
		o.put("totalCount",page.totalCount);
		o.put("startIndex",page.startIndex);
		o.put("itemCount",page.itemCount);
        //result.setPage(o, page, "state");
        result.put("data", o);
	    return result;
	}

	
	/**
    public List<ProductInfo> coverToProduct(List<XmlData> list){
    	List<ProductInfo> l = new ArrayList<ProductInfo>();
    	for(XmlData d:list){
    		ProductInfo p = new ProductInfo(-1, d.deal_name, d.deal_img, d.deal_wap_url, "001003", "", "", "", "", "", null, d.value, d.price);
    		if(!StringUtil.isEmpty(latitude,longitude)){
    			double distance = DistanceEstimator.route(latitude, longitude, "", d.deal_lat, d.deal_lng, "");
    			p.distance=distance;
    		}
    		
    		l.add(p);
    	}
    	return l;
    }
    */
	
}
